toNegativeIntOrThrow

@ExperimentalSinceKotoolsTypes(version = "4.3.1")
fun Number.toNegativeIntOrThrow(): NegativeInt

Returns this number as a NegativeInt, which may involve rounding or truncation, or throws IllegalArgumentException if this number is strictly positive.

var result: NegativeInt = (-1).toNegativeIntOrThrow()
println(result) // -1

result = 0.toNegativeIntOrThrow()
println(result) // 0

1.toNegativeIntOrThrow() // IllegalArgumentException

You can use the toNegativeIntOrNull function for returning null instead of throwing an IllegalArgumentException when this number is strictly positive.